<?php

/**
 * 数组结构变更
 * construction changes
 * @param type $arr
 * @param type $key
 * @param type $val
 */
function array_cc($arr, $key = '', $val = '') {
    $list = array();
    if (!is_array($arr))
        return $list;


    foreach ($arr as $v) {

        if (emptys($key))
            $list[] = emptys($val) ? $val : $v[$val];
        else
            $list[$v[$key]] = emptys($val) ? $v : $v[$val];
    }
    return $list;
}

/**
 * 验证参数是否为空
 * @return boolean
 */
function emptys() {
    $fun = func_get_args();
    foreach ($fun as $v) {
        if ((is_string($v) && strlen($v) == 0) || empty($v)) {
            return true;
            break;
        }
    }
    return false;
}

function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL) {
    $codetype = 'UTF-8';
    $do_dump_indent = "<span style='color:#666666;'>|</span> &nbsp;&nbsp; ";
    $reference = $reference . $var_name;
    $keyvar = 'the_do_dump_recursion_protection_scheme';
    $keyname = 'referenced_object_name';

// So this is always visible and always left justified and readable
    echo "<div style='text-align:left; background-color:white; font: 100% monospace; color:black;'>";

    if (is_array($var) && isset($var[$keyvar])) {
        $real_var = &$var[$keyvar];
        $real_name = &$var[$keyname];
        $type = ucfirst(gettype($real_var));
        echo "$indent$var_name <span style='color:#666666'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
    } else {
        $var = array($keyvar => $var, $keyname => $reference);
        $avar = &$var[$keyvar];

        $type = ucfirst(gettype($avar));
        if ($type == "String")
            $type_color = "<span style='color:green'>";
        elseif ($type == "Integer")
            $type_color = "<span style='color:red'>";
        elseif ($type == "Double") {
            $type_color = "<span style='color:#0099c5'>";
            $type = "Float";
        } elseif ($type == "Boolean")
            $type_color = "<span style='color:#92008d'>";
        elseif ($type == "NULL")
            $type_color = "<span style='color:black'>";

        if (is_array($avar)) {
            $count = count($avar);
            echo "$indent" . ($var_name ? "$var_name => " : "") . "<span style='color:#666666'>$type ($count)</span><br>$indent(<br>";
            $keys = array_keys($avar);
            foreach ($keys as $name) {
                $value = &$avar[$name];
                do_dump($value, "['$name']", $indent . $do_dump_indent, $reference);
            }
            echo "$indent)<br>";
        } elseif (is_object($avar)) {
            echo "$indent$var_name <span style='color:#666666'>$type</span><br>$indent(<br>";
            foreach ($avar as $name => $value)
                do_dump($value, "$name", $indent . $do_dump_indent, $reference);
            echo "$indent)<br>";
        } elseif (is_int($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> $type_color" . htmlspecialchars($avar) . "</span><br>";
        elseif (is_string($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> $type_color\"" . htmlspecialchars($avar) . "\"</span><br>";
        elseif (is_float($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> $type_color" . htmlspecialchars($avar) . "</span><br>";
        elseif (is_bool($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> $type_color" . ($avar == 1 ? "TRUE" : "FALSE") . "</span><br>";
        elseif (is_null($avar))
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> {$type_color}NULL</span><br>";
        else
            echo "$indent$var_name = <span style='color:#666666'>$type(" . mb_strlen($avar, $codetype) . ")</span> " . htmlspecialchars($avar) . "<br>";

        $var = $var[$keyvar];
    }
    echo "</div>";
}

function dp() {
    header("Content-type : text/html; charset=UTF-8");
    $funlist = func_get_args();
    $exitstring = 'exit'; //如果包含EXIT执行完后则停止
    foreach ($funlist as $key => $val) {
        if ($val == $exitstring)
            continue;
        do_dump($val, $key + 1);
        echo "<hr/>";
    }
    if (array_search($exitstring, $funlist) !== false)
        exit();
}

/**
 *  取出数组指定的key
 * @param type $array
 * @param type $keys
 * @param type $except 是否为排除
 */
function getArrayToKey($array, $keys, $except = false) {
    $keys = is_array($keys) ? $keys : explode(',', $keys);

    $new_array = array();
    foreach ($array as $key => $list) {
        foreach ($list as $key2 => $value) {

            if ($except == true && in_array($key2, $keys))
                continue;

            if ($except == false && !in_array($key2, $keys))
                continue;

            $new_array[$key][$key2] = $value;
        }
    }

    return $new_array;
}

//经纬度之间距离计算
function rad($d) {
    return $d * 3.1415926535898 / 180.0;
}

function GetDistance($lat1, $lng1, $lat2, $lng2) {
    $EARTH_RADIUS = 6378.137;
    $radLat1 = rad($lat1);
    //echo $radLat1;
    $radLat2 = rad($lat2);
    $a = $radLat1 - $radLat2;
    $b = rad($lng1) - rad($lng2);
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) +
                            cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * $EARTH_RADIUS;
    $s = round($s * 10000) / 10000;
    return $s;
}
